//
//  OJIssuesListController.m
//  openJira
//
//  Created by Vincent Demay on 29/04/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "OJIssuesListController.h"
#import "OJIssueViewController.h"


@implementation OJIssuesListController

@synthesize filter;


- (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minilogo.png"]];
		self.navigationItem.titleView = imageView;
		[imageView release];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
	
	activity = [[TTActivityLabel alloc] initWithStyle:TTActivityLabelStyleBlackBezel];
	activity.text = @"Loading...";
	[activity sizeToFit];
	activity.frame = CGRectMake(0, 0, self.view.width, self.view.height);
	[self.tableView insertSubview:activity aboveSubview:self.parentViewController.view];
	activity.isAnimating = YES;
	
	model = [[OJIssueList alloc] init];
	model.delegate = self;
	[model getIssuesFromFilter:filter];
}

- (void) object:(OJIssueList*) filter isReady:(BOOL) onError {
	activity.isAnimating = NO;
	activity.hidden = YES;
	
	if (onError) {
		//failed -> do something
	} else {
		[self.tableView reloadData];
	}
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [model.issues count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 70.0;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	OJIssue * currentIssue = (OJIssue*)[model.issues objectAtIndex:indexPath.row];
	
    static NSString *CellIdentifier = @"Cell";
	
	static NSInteger imageTag = 1;
	static NSInteger titleTag = 2;
	static NSInteger descriptionTag = 3;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		
		TTImageView * image = [[TTImageView alloc] initWithFrame:CGRectMake(5, 5, 15, 15)];
		image.tag = imageTag;
		[cell.contentView addSubview:image];
		[image release];
		
		UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 5, cell.contentView.frame.size.width - 25 , 15)];
		title.font = [UIFont boldSystemFontOfSize:16];
		title.textColor = BG_COLOR;
		title.tag = titleTag;
		[cell.contentView addSubview:title];
		[title release];
		
		UILabel *description = [[UILabel alloc] initWithFrame:CGRectMake(5, title.bottom + 5, cell.contentView.frame.size.width - 10, 40)];
		description.lineBreakMode = UILineBreakModeWordWrap;
		description.font = [UIFont systemFontOfSize:14];
		description.textColor = GREY_COLOR;
		description.numberOfLines = 0;
		description.tag = descriptionTag;
		[cell.contentView addSubview:description];
		[description release];
    }
	
	TTImageView * image = (TTImageView *) [cell.contentView viewWithTag:imageTag];
	image.urlPath = currentIssue.priority.icon;
	
	UILabel * title = (UILabel *) [cell.contentView viewWithTag:titleTag];
	title.text = currentIssue.key;
	
	UILabel * description = (UILabel *) [cell.contentView viewWithTag:descriptionTag];
	description.text = currentIssue.summary;
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	OJIssue * currentIssue = (OJIssue*)[model.issues objectAtIndex:indexPath.row];
	
	OJIssueViewController *viewController = [[OJIssueViewController alloc] initWithIssue:currentIssue];
	[self.navigationController pushViewController:viewController animated:YES];
	[viewController release];
}


- (void)dealloc {
	[activity dealloc];
	[model dealloc];
	[filter dealloc];
    [super dealloc];
}


@end

